# coding:utf-8
from pangolin.common.db_operateCommon import DbOperateClass
import os


def query_from_cpa_miss_product():
    db_operate = DbOperateClass()
    sql = "select product.url,product.task_id,product.param_code,product.task_date,product.product_id,product.city_code,product.id from cpa_miss_product product where  product.deal_flag='0' and product.param_code!='TC_Ticket_Grab_M'"  # 临时排除重跑TC M站的脚本
    (fc, count) = db_operate.select_by_sql(sql)
    for row in fc:
        url = row[0]
        task_id = row[1]
        timer_param = row[2]
        task_date = row[3]
        lv_product_id = row[4]
        city_code = row[5]
        id = row[6]
        company_code, spider_name = query_from_cpa_config(timer_param)
        sql = "SELECT product.deal_flag FROM cpa_miss_product product WHERE product.id = '" + str(id) + "'"
        data = db_operate.select_one_by_sql(sql)
        if data[0] is not None and data[0] == 0:
            cmd = "python " + os.path.split(os.path.realpath(__file__))[0] + "/miss_main.py" + " " + city_code + \
                  " " + str(company_code) + " " + task_id + " " + str(task_date) + " " + str(spider_name) + " " + \
                  timer_param + " " + url + " " + lv_product_id
        os.system(cmd)


def query_from_cpa_config(timer_param):
    company_code = None
    spider_name = None
    if timer_param != "":
        db_operate = DbOperateClass()
        company_code_sql = "select paramvalue, configname from cpa_config  where configname like '" + timer_param + "' and paramname = 'COMPANY_CODE'"
        data = db_operate.select_one_by_sql(company_code_sql)
        if data is not None:
            company_code = data[0]
        spider_name_sql = "select paramvalue from cpa_config  where configname like '" + timer_param + "' and paramname = 'EXEC_PYTHON_FILE'"
        data = db_operate.select_one_by_sql(spider_name_sql)
        if data is not None:
            spider_name = data[0]
    return company_code, spider_name

if __name__ == '__main__':
    query_from_cpa_miss_product()
